﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmLineaTelefonica : Form
    {
        [Dependency]
        public ILineaTelefonicaBL lineaBL { get; set; }

    [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmLineaTelefonica()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public USUARIO currentUsuarioLineaTelefonica { get; set; }

        private void FrmLineaTelefonica_Load(object sender, EventArgs e)
        {
            if (currentUsuarioLineaTelefonica != null)
            {
                menuLlena();
            }
            else
                CargarDatos();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioLineaTelefonica.PERSONA.Dni);

            var permisos = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.UsuarioId)
                            select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[] { 
            this.BtnEliminar,this.BtnEditar,this.BtnNuevo};

            var noVisible = from nv in botones
                            where !permisos.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            CargarDatos();
        }

        private void CargarDatos()
        {
            lINEA_TELEFONICABindingSource.DataSource = lineaBL.GetAllFromLineaTelefonica();
        }

        private void BtnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoLineaTelefonica mttoLinea = new FrmMttoLineaTelefonica();
            mttoLinea.Nuevo();
            mttoLinea.ShowDialog();
            CargarDatos();
        }

        private void BtnEditar_Click(object sender, EventArgs e)
        {
            LINEA_TELEFONICA currentlinea = lINEA_TELEFONICABindingSource.Current as LINEA_TELEFONICA;
            
            if (currentlinea != null)
            {
                FrmMttoLineaTelefonica mttoLinea = new FrmMttoLineaTelefonica();
                mttoLinea.currentlinea = currentlinea;
                mttoLinea.Editar();
                mttoLinea.ShowDialog();
                CargarDatos();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Libro", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            lINEA_TELEFONICABindingSource.DataSource = lineaBL.GetAllFromLineaTelefonicaByDescripcion(TxtCriterio.Text);
        }

        private void TxtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lINEA_TELEFONICABindingSource.DataSource = lineaBL.GetAllFromLineaTelefonicaByDescripcion(TxtCriterio.Text);
            }
        }

        private void BtnEliminar_Click(object sender, EventArgs e)
        {
            LINEA_TELEFONICA currentlinea = lINEA_TELEFONICABindingSource.Current as LINEA_TELEFONICA;
            if (currentlinea != null)
            {
                var rpt = MessageBox.Show(this,
                   string.Format("¿Desea eliminar la Línea Telefónica: {0}?", currentlinea.Descripcion),
                   "Mensaje",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);

                if (rpt == DialogResult.Yes)
                {
                    lineaBL.DeleteLinea(currentlinea.Id);
                    CargarDatos();
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar una Línea.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void TxtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
